package com.draven.mall.controller;

import com.draven.mall.common.ApiResponse;
import com.draven.mall.entity.Rider;
import com.draven.mall.service.RiderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/rider")
public class RiderController {

    private static final Logger log = LoggerFactory.getLogger(RiderController.class);

    @Autowired
    private RiderService riderService;

    /**
     * 获取骑手详情
     */
    @GetMapping("/detail")
    public ApiResponse getRiderDetail(@RequestParam Long riderId) {
        try {
            log.info("获取骑手详情, riderId={}", riderId);
            Rider rider = riderService.getById(riderId);
            if (rider == null) {
                log.warn("骑手不存在, riderId={}", riderId);
                return ApiResponse.error("骑手不存在");
            }
            
            // 出于安全考虑，移除敏感信息
            rider.setPassword(null);
            rider.setIdCard(null);
            
            log.info("骑手详情获取成功, riderId={}, rating={}, completedOrderCount={}", 
                riderId, rider.getRating(), rider.getCompletedOrderCount());
            return ApiResponse.success(rider);
        } catch (Exception e) {
            log.error("获取骑手详情异常", e);
            return ApiResponse.error("获取骑手详情失败：" + e.getMessage());
        }
    }

    /**
     * 获取骑手评价列表
     */
    @GetMapping("/ratings")
    public ApiResponse getRiderRatings(
            @RequestParam Long riderId, 
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size,
            @RequestParam(required = false) String rating) {
        try {
            log.info("获取骑手评价列表, riderId={}, page={}, size={}, rating={}", 
                    riderId, page, size, rating);
            
            if (riderId == null) {
                return ApiResponse.error("骑手ID不能为空");
            }
            
            // 验证骑手是否存在
            Rider rider = riderService.getById(riderId);
            if (rider == null) {
                log.warn("骑手不存在, riderId={}", riderId);
                return ApiResponse.error("骑手不存在");
            }
            
            // 获取评价列表
            try {
                Map<String, Object> result = riderService.getRiderRatings(riderId, page, size, rating);
                log.info("骑手评价列表获取成功, riderId={}, total={}", 
                        riderId, result.get("total"));
                return ApiResponse.success(result);
            } catch (Exception e) {
                log.error("获取骑手评价列表服务异常", e);
                // 返回空结果
                Map<String, Object> emptyResult = new HashMap<>();
                emptyResult.put("records", new ArrayList<>());
                emptyResult.put("total", 0);
                emptyResult.put("pages", 0);
                emptyResult.put("current", page);
                emptyResult.put("size", size);
                emptyResult.put("statistics", new HashMap<String, Object>() {{
                    put("excellentCount", 0L);
                    put("goodCount", 0L);
                    put("badCount", 0L);
                    put("ratingDetails", new HashMap<>());
                }});
                return ApiResponse.success(emptyResult);
            }
        } catch (Exception e) {
            log.error("获取骑手评价列表异常", e);
            return ApiResponse.error("获取骑手评价列表失败：" + e.getMessage());
        }
    }
} 